﻿using System.Collections.Generic;
using Mal.Balades.Admin.Services;

namespace Mal.Balades.Admin.Repositories
{
    public class PictureTypeRepository: RepositoryBase
    {
        #region Fields
        IAdminService service;
        private List<IPictureType> pictureTypes;       
        #endregion

        #region Construction / Destruction
        public PictureTypeRepository()
        {
        }

        ~PictureTypeRepository()
        {
            Cleanup();
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public List<IPictureType> Datas
        {
            get { return pictureTypes; }
            private set { pictureTypes = value; }
        }
        #endregion

        #region Methods
        public void Cleanup()
        {
            if (this.service != null)
            {
                service.GetPictureTypesCompleted -= new ServiceResponse<List<IPictureType>>(OnGetPictureTypesCompleted);
                this.service.Cleanup();
                this.service = null;
            }
        }

        private void Initialize()
        {
            pictureTypes = new List<IPictureType>();
            service = AdminServiceFactory.GetInstance();
            service.GetPictureTypesCompleted += new ServiceResponse<List<IPictureType>>(OnGetPictureTypesCompleted);
        }

        protected override void LoadRepository()
        {
            Initialize();

            this.service.GetPictureTypesAsync();
        }
        #endregion

        #region Events
        void OnGetPictureTypesCompleted(ServiceResponseArgs<List<IPictureType>> e)
        {
            this.Cleanup();

            if (e.Error != null)
                throw e.Error;

            this.Datas = e.Result;

            this.LoadCompleted();
        }
        #endregion
    }
}
